package com.jwsoft.manager.core.dao;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.po.TableField;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import javax.validation.constraints.NotNull;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/** 代码生成器支持自定义[DTO\VO等]模版 */
public final class EnhanceFreemarkerTemplateEngine extends FreemarkerTemplateEngine {

    @Override
    protected void outputCustomFile(
            @NotNull Map<String, String> customFile,
            @NotNull TableInfo tableInfo,
            @NotNull Map<String, Object> objectMap) {
        String entityName = tableInfo.getEntityName();
        String otherPaths = this.getPathInfo(OutputFile.other);
        List<String> otherPathList =
                Arrays.stream(otherPaths.split(",")).collect(Collectors.toList());
        List<TableField> fields = tableInfo.getFields();
        fields.forEach(
                item -> {
                    if (item.isKeyFlag()) {
                        objectMap.put(
                                "getId",
                                "get" + StrUtil.upperFirst(StrUtil.toCamelCase(item.getName())));
                        objectMap.put("id", StrUtil.toCamelCase(item.getName()));
                        objectMap.put(
                                "setId",
                                "set" + StrUtil.upperFirst(StrUtil.toCamelCase(item.getName())));
                    }
                });
        customFile.forEach(
                (key, value) -> {
                    if ("VO".equals(key) || "SaveVO".equals(key) || "KeyVO".equals(key)|| "QueryVO".equals(key)) {
                        objectMap.put(
                                key,
                                objectMap.get("voPackageName")
                                        + "."
                                        + StrUtil.lowerFirst(entityName));
                        String fileName =
                                String.format(
                                        otherPathList.get(0)
                                                + File.separator
                                                + StrUtil.lowerFirst(entityName)
                                                + File.separator
                                                + entityName
                                                + "%s",
                                        key + ".java");
                        this.outputFile(
                                new File(fileName),
                                objectMap,
                                value,
                                this.getConfigBuilder().getInjectionConfig().isFileOverride());
                    }
                });
        customFile.forEach(
                (key, value) -> {
                    if ("Integration".equals(key) || "IntegrationImpl".equals(key)) {
                        objectMap.put(
                                key,
                                objectMap.get("integrationPackageName")
                                        + "."
                                        + StrUtil.lowerFirst(entityName));
                        String fileName =
                                String.format(
                                        otherPathList.get(1)
                                                + ("IntegrationImpl".equals(key)
                                                ? File.separator + "impl"
                                                : "")
                                                + File.separator
                                                + entityName
                                                + "%s",
                                        key + ".java");
                        this.outputFile(
                                new File(fileName),
                                objectMap,
                                value,
                                this.getConfigBuilder().getInjectionConfig().isFileOverride());
                    }
                });
        customFile.forEach(
                (key, value) -> {
                    if ("Controller".equals(key)) {
                        objectMap.put(
                                key,
                                objectMap.get("controllerPackageName")
                                        + "."
                                        + StrUtil.lowerFirst(entityName));
                        String fileName =
                                String.format(
                                        otherPathList.get(2) + File.separator + entityName + "%s",
                                        key + ".java");
                        this.outputFile(
                                new File(fileName),
                                objectMap,
                                value,
                                this.getConfigBuilder().getInjectionConfig().isFileOverride());
                    }
                });
        customFile.forEach(
                (key, value) -> {
                    if ("vue3Index".equals(key)) {
                        String fileName =
                                System.getProperty("user.dir") + "/views/" + objectMap.get("fileName") + File.separator
                                        + objectMap.get("funcCode") + "/index.vue";
                        this.outputFile(
                                new File(fileName),
                                objectMap,
                                value,
                                this.getConfigBuilder().getInjectionConfig().isFileOverride());
                    }
                    if ("vue3Form".equals(key)) {
                        String fileName =
                                System.getProperty("user.dir") + "/views/" + objectMap.get("fileName") + File.separator
                                        + objectMap.get("funcCode") + "/component/" + objectMap.get("funcCode") + "Form.vue";
                        this.outputFile(
                                new File(fileName),
                                objectMap,
                                value,
                                this.getConfigBuilder().getInjectionConfig().isFileOverride());
                    }
                });
    }
}
